package com.szy.douyou_article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.szy.douyou_article.config.ErrorMsgEnum;
import com.szy.douyou_article.config.ResultDTO;
import com.szy.douyou_article.dao.Article;
import com.szy.douyou_article.dao.Comment;
import com.szy.douyou_article.dao.State;
import com.szy.douyou_article.dao.mapper.ArticleMapper;
import com.szy.douyou_article.dao.mapper.CommentMapper;
import com.szy.douyou_article.dao.req.ArticleReq;
import com.szy.douyou_article.service.ArticleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author sunziyan
 * @date 2022/3/1
 */
@Service
public class ArticleServiceImpl implements ArticleService {

    @Resource
    private ArticleMapper articleMapper;

    @Resource
    private CommentMapper commentMapper;


    @Override
    public ResultDTO<Page<Article>> queryAllArticle(ArticleReq articleReq) {
        Page<Article> articlePage = new Page<>(articleReq.getPageNo(), articleReq.getPageSize());
        //分页并按时间排序
        Page<Article> articleList = articleMapper.pageQueryArticleByArticleReq(articlePage, articleReq);
        return ResultDTO.success(articleList);
    }

    @Override
    public ResultDTO addOneArticle(Article article) {
        int result = articleMapper.insert(article);
        if (result == 0) {
            return ResultDTO.fail(ErrorMsgEnum.INSERT_FAIL.desc());
        }

        if (isNumeric(article.getClassificationName())) {
            Integer result1 = articleMapper.insertClassificationArticle(article);
            if (result1 == 0) {
                return ResultDTO.fail("新增分类和帖子表失败");
            }
        }
        return ResultDTO.success(result);
    }

    @Override
    public ResultDTO<Article> queryOneArticle(Long articleId) {


        Article article = articleMapper.selectById(articleId);

        if (article == null) {
            return ResultDTO.fail("找不到此文章");
        }
        ResultDTO<Article> resultDTO = ResultDTO.success(article);

        return resultDTO;

    }

    @Override
    public ResultDTO updateOneArticle(Article article) {

        if (isNumeric(article.getClassificationName())) {
            Integer result = articleMapper.updateClassificationArticle(article);
            if (result == 0) {
                articleMapper.insertClassificationArticle(article);
            }
        }


        int result = articleMapper.updateById(article);

        if (result == 0) {
            return ResultDTO.fail("修改失败");
        }

        return ResultDTO.success(null);

    }


    /**
     * 匹配是否为数字
     *
     * @param str 可能为中文，也可能是-19162431.1254，不使用BigDecimal的话，变成-1.91624311254E7
     * @return
     * @author yutao
     * @date 2016年11月14日下午7:41:22
     */
    public static boolean isNumeric(String str) {
        // 该正则表达式可以匹配所有的数字 包括负数
        Pattern pattern = Pattern.compile("-?[0-9]+(\\.[0-9]+)?");
        String bigStr;
        try {
            bigStr = new BigDecimal(str).toString();
        } catch (Exception e) {
            //异常 说明包含非数字。
            return false;
        }

        // matcher是全匹配
        Matcher isNum = pattern.matcher(bigStr);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }

    @Override
    public ResultDTO deleteOneArticle(Long articleId) {

        Integer articleResult = articleMapper.deleteById(articleId);

        Integer classificationResult = articleMapper.deleteClassificationArticleByArticleId(articleId);

        Integer commentResult = commentMapper.delete(new QueryWrapper<Comment>().eq("article_id",articleId));
        if (articleResult == 0) {
            return ResultDTO.fail("删除文章表失败");
        } else if (classificationResult == 0) {
            return ResultDTO.fail("删除分类文章关系表失败");
        }

        return ResultDTO.success(1);
    }

    @Override
    public ResultDTO addArticleViews(Long articleId) {
        Integer result = articleMapper.updateArticleViews(articleId);
        if (result == 0){
            return ResultDTO.fail("浏览数增加失败");
        }
        return ResultDTO.success();
    }

    @Override
    public ResultDTO getArticleAllState(Long articleId) {
        State state = new State();
        state.setArticleId(articleId);
        state.setArticleGoodCount(articleMapper.getArticleGoodStateCount(state));
        state.setArticleBadCount(articleMapper.getArticleBadStateCount(state));
        return ResultDTO.success(state);
    }

    @Override
    public ResultDTO getMyArticleState(Long articleId,Long userId) {
        State state = new State();
        state.setArticleId(articleId);
        state.setUserId(userId);
        state.setArticleUserState(articleMapper.getMyArticleState(state));

        return ResultDTO.success(state);
    }

    @Override
    public ResultDTO createArticleState(State state) {
        Integer result = articleMapper.insertArticleState(state);
        return ResultDTO.success(result);
    }

    @Override
    public ResultDTO changeArticleState(State state) {
        Integer result = articleMapper.updateArticleState(state);
        return ResultDTO.success(result);
    }

}
